<?php

require_once(realpath(dirname(__FILE__)) . '/../config.php');
require_once(realpath(dirname(__FILE__)) . '/../includes/common/ServiceFactory.php');

Session::start();

$config = ServiceFactory::get("SystemConfiguration");
$userService = ServiceFactory::get("UserService");
$authService = ServiceFactory::get("AuthenticationService");

$action = $_REQUEST['action'];
$redirect = $_REQUEST['back'];

if(empty($redirect))
    $redirect = "./index.php";

if ( empty($action) ) 
{
    if (isset($_POST['login']) && !empty($_POST['login']))
        $action = 'login';
}

switch($action)
{
    case 'login':
        $name = $_REQUEST['userLoginName'];
        $pass = $_REQUEST['userLoginPass'];
        
        if(empty($name))
            RedirectMessage::info("Bạn chưa nhập Tên", "Error");
        elseif(empty($pass))
            RedirectMessage::info("Bạn chưa nhập Mật khẩu", "Error");
        else
        {
            try
            {
                $user = $userService -> findUserByName($name);
                if($user->getActivationKey() != null)
                {
                    RedirectMessage::info("Tài khoản '$name' cần được kích hoạt trước khi sử dụng", "Error");
                    Response::redirect($_SERVER['REQUEST_URI']);
                }
                
                if($user->getPassword() != sha1($pass))
                {
                    RedirectMessage::info("Mật khẩu không đúng", "Error");
                    Response::redirect($_SERVER['REQUEST_URI']);
                }
                
                $authService -> login($user);
                Response::redirect($redirect);
            }
            catch(Exception $ex)
            {
                RedirectMessage::info($ex->getMessage(), "Error");
            }
        }
        
        Response::redirect($_SERVER['REQUEST_URI']);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="">
<head>
    <title><?php echo $config->getSetting("site-title") . " - Login"; ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">
        function focusit() {
            document.getElementById('userLoginName').focus();
        }
        window.onload = focusit;
    </script>
    <link rel='stylesheet' href='css/global.css' type='text/css' />
    <link rel='stylesheet' href='css/admin.css' type='text/css' />
    <link rel='stylesheet' href='css/login.css' type='text/css' />
</head>
<body>
    <div class="TopHeaderNav"><a href="../">Back to Now!chat</a></div>
    <div id="login">
        <h1><a title="Now!chat" href="<?php echo $config->getSetting("site-url"); ?>">Now!chat</a></h1>
        <?php 
            if(RedirectMessage::hasMessage())
            {
                InfoArea::showInfo(RedirectMessage::getMessage(), RedirectMessage::getMessage('title'), RedirectMessage::getMessage('type'));
                RedirectMessage::removeMessage();
            }
        ?>
        <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>" id="loginForm" name="loginForm">
            <p>
                <label for="log">Username</label><br/>
                <input type="text" tabindex="10" size="20" value="" class="Input" id="userLoginName" name="userLoginName"/>
            </p>
            <p>
                <label for="pwd">Password</label><br/>
                <input type="password" tabindex="20" size="20" value="" class="Input" id="userLoginPass" name="userLoginPass"/>
            </p>
            <p class="Submit">
                <input type="submit" tabindex="100" value="Log In" id="submit" name="login"/>
                <input type="hidden" name="back" value="<?php echo $redirect ?>" />
            </p>
        </form>
    </div>
</body>
</html>